package demo1;

public class MysingList {
     class ListNode{
        public int val;// 节点的值
        public ListNode next;//节点的地址
        //创建构造方法进行初始化
        public ListNode(int val) {
            this.val = val;
        }
    }
        public ListNode head;//永远指向我的头节点

    public void createList(){//创建链表
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);

        //将链表的地址链接起来
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        this.head = node1;
    }

    //打印单链表
    public void show(){
        ListNode cur = head;//将头结点记录下来、
        while(cur.next != null){
            System.out.print(cur.val +"  ");
            cur = cur.next;
        }
        System.out.println();
    }

    //头插法
    public void addFist(){
        ListNode node11 = new ListNode(99); //初始化一个新的节点（产生一个新的节点）
        node11.next = head;//将新节点的next 和 头节点相连接
        head = node11;//头节点前移
    }

    //尾插法
    public void addLast(){
        ListNode node22 = new ListNode(88);
        ListNode cur = head;
        if(head == null){
            return;
        }
        while (cur.next != null){
            cur = cur.next;
        }
        cur.next = node22;
    }

    //得到单链表的长度
    public int size(){
        int count = 0;
        ListNode cur = head;
        while(cur.next != null){
            count++;
            cur = cur.next;
        }
        return count;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        ListNode node77 = new ListNode(data);
        //判断下标是否合理
        if(index < 0 || index > size()){
            throw new RuntimeException("index的下标不合理！！");
        }
        if (head == null){
            addFist();
            return;
        }
        if(index == size()){
            addLast();
            return;
        }
        ListNode cur = search(50);



    }
    //找到index下标
    public ListNode search(int index){
        ListNode cur = head;
        while(index - 1 != 0){
            cur = cur.next;
        }
        return cur;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
    }
}
